package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.manager.system.BiChannelManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.SysRoleChannelManager;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.*;

/**
 * 渠道配置管理
 * 
 * @author marin
 * @date 2015-09-29
 */
@Controller("channelConfigAction")
@Scope("view")
public class ChannelConfigAction {
	@Autowired
	private BiPlatformManager biPlatformManager;
	@Autowired
	private BiChannelManager biChannelManager;
    @Autowired
    private SysRoleChannelManager sysRoleChannelManager;

	private EntityBiChannel entity;
	private List<EntityBiChannel> list;
	private Map<String, String> allItmes;  // 平台下拉列表

	@Autowired
	public void init() {
		initData();
	}
	
	private void initData() {
		entity = new EntityBiChannel();
        list = BiPlatformManager.CHANNEL_LIST;
		allItmes = new LinkedHashMap<>();
		allItmes.put("请选择", "0");
		for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
			allItmes.put(p.getName(), p.getId());
		}
	}

	public void search() {
		List<Object> cons = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" from EntityBiChannel l where 1=1 ");
		if (!Utils.isEmptyStr(entity.getId())) {
			hql.append(" and l.id = ? ");
			cons.add(entity.getId());
		}
		if (!Utils.isEmptyStr(entity.getName())) {
			hql.append(" and l.name = ? ");
			cons.add(entity.getName());
		}
        if (!Utils.isEmptyStr(entity.getPlatName())) {
            hql.append(" and l.platName = ? ");
            cons.add(entity.getPlatName());
        }
		if (!Utils.isEmptyStr(entity.getPlatformId()) && !"0".equals(entity.getPlatformId())) {
			hql.append(" and l.platformId = ? ");
			cons.add(entity.getPlatformId());
		}
		hql.append(" order by l.id");
		
		list = biChannelManager.findByQuery(hql.toString(), cons.toArray());
		Collections.sort(list, new EntityBiChannel().comparatorId);
	}
	
	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, entity, true, list)) {
		    entity.setGameId(0);
			biChannelManager.persist(entity);
			context.addMessage(null, new FacesMessage("添加成功！"));
			
			biPlatformManager.init();
			initData();
		}
	}

	public void modify(EntityBiChannel e) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (validate(context, e, false, list)) {
		    e.setGameId(0);
			biChannelManager.merge(e);
			context.addMessage(null, new FacesMessage("修改成功！"));
			
			biPlatformManager.init();
			initData();
		}
	}
	
	public void delete(String id) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (!Utils.isEmptyStr(id)) {
			List<String> cl = BiPlatformManager.CHANNEL_SERVERS_MAP.get(id);
			if (cl != null && cl.size() > 0) {
				context.addMessage(null, new FacesMessage("此渠道与区服有关联，不能删除！"));
				return;
			}
            //检查角色分配
			if(sysRoleChannelManager.hasChannelAssigned(id)){
                context.addMessage(null, new FacesMessage("此渠道与角色有关联，不能删除！"));
                return;
            }
			biChannelManager.removeById(id);
		}
		
		context.addMessage(null, new FacesMessage("删除成功！"));

		biPlatformManager.init();
		initData();
	}

	private boolean validate(FacesContext context, EntityBiChannel ebp, boolean isAdd, List<EntityBiChannel> ebpList) {
		if (Utils.isEmptyStr(ebp.getId())) {
			context.addMessage(null, new FacesMessage("渠道ID不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getName())) {
			context.addMessage(null, new FacesMessage("渠道名称不能为空！"));
			return false;
		}
		if (Utils.isEmptyStr(ebp.getPlatformId()) || "0".equals(ebp.getPlatformId())) {
			context.addMessage(null, new FacesMessage("渠道平台不能为空！"));
			return false;
		}
		if (isAdd) {  // 如果是添加，则ID与名称皆不能重
			for (EntityBiChannel e : ebpList) {
				if (ebp.getId().equals(e.getId())) {
					context.addMessage(null, new FacesMessage("渠道ID重复！"));
					return false;
				}
				if (ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("渠道名称重复！"));
					return false;
				}
			}
		} else {  // 如果是修改，则名称不能重
			for (EntityBiChannel e : ebpList) {
				if (!ebp.getId().equals(e.getId()) && ebp.getName().equals(e.getName())) {
					context.addMessage(null, new FacesMessage("渠道名称重复！"));
					return false;
				}
			}
		}
		
		return true;
	}
	
	public EntityBiChannel getEntity() {
		return entity;
	}

	public void setEntity(EntityBiChannel entity) {
		this.entity = entity;
	}

	public List<EntityBiChannel> getList() {
		return list;
	}

	public void setList(List<EntityBiChannel> list) {
		this.list = list;
	}
	
	public Map<String, String> getAllItmes() {
		return allItmes;
	}

	public void setAllItmes(Map<String, String> allItmes) {
		this.allItmes = allItmes;
	}
}
